<template>
  <a-modal :title="title" v-model="visible" destroyOnClose :confirmLoading="confirmLoading" @ok="handleOk" width="600px">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="{ span: 4 }" :wrapper-col="{ span: 20 }">
        <a-form-model-item label="角色名称">
          <a-input v-model="form.roleName" :disabled="disabled" />
        </a-form-model-item>
        <a-form-model-item label="权限字符">
          <a-input v-model="form.roleCode" :disabled="disabled" />
        </a-form-model-item>
        <a-form-model-item label="权限范围">
          <a-tree-select :tree-data="scopedtList" v-model="form.dataScope" :defaultValue="form.dataScope"></a-tree-select>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
// import { pick } from 'lodash'
export default {
  name: 'RolePerModal',
  props: ['url'],
  data () {
    return {
      title: '操作',
      disabled: false,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      disableSubmit: false,
      form: {},
      scopedtList: [
        { value: '1', title: '全部数据权限', key: 1 },
        { value: '2', title: '工作人员数据权限', key: 2 }
      ],
      rules: {

      },
    }
  },
  created () {
  },
  computed: {

  },
  watch: {

  },
  methods: {
    add () {
      this.visible = true
      this.title = '新增角色'
      this.form = {}
    },
    edit (record) {
      this.disabled = false
      this.visible = true
      this.title = '修改数据权限'
      this.form = record
    },
    close () {
      this.$emit('close')
      this.visible = false
    },
    handleNodeClick (e) {
      console.log(e)
    },
    sortNoChange (value) {
      console.log(value)
    },
    handleOk () {
      // 触发表单验证
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.confirmLoading = true
          let url = ''
          if (!this.form.id) {
            url = this.url.add
          } else {
            url = this.url.edit
          }
          this.$post(url, this.form).then(res => {
            if (this.isSuccess(res)) {
              this.confirmLoading = false
              this.$message.success('操作成功')
              this.$emit('ModalOk')
              this.close()
            } else {
              this.$message.error('操作失败')
              this.confirmLoading = false
            }
          }).catch(error => {
            console.log(error)
            this.confirmLoading = false
          })
        } else {
          return false
        }
      })
    },
    handleCancel () {
      this.close()
    },
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    }
  }
}
</script>

<style scoped>
</style>
